<?php
namespace App\Admin\Models;
use Common\BaseModel;
class SourceMaterial extends BaseModel
{
    protected $res;
    protected $redis_key="admin_";
    public function getSource()
    {
        return "source_material";
    }
    public function initialize()
    {
        parent::initialize();
    }



    /**
     * 根据ids获取素材
     * @param $ids
     * @return mixed
     */
    public function get_route_list($ids){
        if(empty($ids)){
            $ids = -1;
        }
        $conditions = "id in({$ids})";
        $user_list = $this::find(
            array(
                $conditions,
                'order'=>'sort desc,id asc',
            )
        );
        return $user_list;
    }

    /**
     * 获取所有的素材
     */
    public function get_path_list($params=[],$method='list'){
        $page = intval($params['page']);
        $size = intval($params['limit']);
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }
        $conditions = "";
        if($params['keywords']){
            $conditions .= "name like '%{$params['keywords']}%'";
        }
        if(isset($params['type']) && $params['type'] == 1){
            if($conditions){
                $conditions .= " AND type=1";
            }else{
                $conditions = "type=1";
            }
        }
        if($method == 'list'){
            $user_list = $this::find(
                array(
                    $conditions,
                    'order'=>'id desc',
                    'limit'  => $size,
                    'offset' => $page
                )
            )->toarray();
            return $user_list;
        }else{
            $count = $this->count(
                array(
                    $conditions,
                    'order'=>'id desc',
                )
            );
            return $count;
        }
    }


    /**
     * 获取所有的素材
     */
    public function get_App_path_list($data=[],$mehotd='list'){
        $conditions = "";
        $page = intval($data['page']);
        $size = intval($data['limit']);
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }
        if(isset($data['keywords']) && !empty($data['keywords'])){
            $conditions .= "name like '%{$data['keywords']}%'";
        }
        if($mehotd == 'list'){
            $user_list = $this::find(
                array(
                    $conditions,
                    "group"=>"app_path",
                    "cloumns"=>"app_path",
                    'order'=>'id desc',
                    'limit'  => $size,
                    'offset' => $page
                )
            );
            return $user_list;
        }else{
            $count = $this->count(
                array(
                    $conditions,
                    "group"=>"app_path",
                    "cloumns"=>"app_path",
                    'order'=>'id desc',
                )
            );
            return $count;
        }
    }



    /**
     * @func 根据素材id获取素材
     * @param $id
     * @return array
     */
    public function find_by_id($id){
        if(empty($id)){
            return [];
        }
        $cate_info = $this->findFirstById($id);
        if($cate_info){
            return $cate_info->toarray();
        }else{
            return [];
        }
    }


    /**
     * @func 根据素材id获取素材
     * @param $id
     * @return array
     */
    public function find_by_id_obj($id){
        return $this->findFirstById($id);
    }

    public function save_params($params){
        $info['name']=$params['name'];
        $info['path']=$params['path'];
        $info['app_path']=$params['app_path'];
        $info['type']=$params['type'];
        $info['url']=$params['url'];
        $info['app_data']=$params['app_data'];
        $info['is_tb_atuh']=$params['is_tb_atuh'];
        $info['is_need_login']=$params['is_need_login'];
        $info['notice_id']=$params['notice_id'];
        $info['start_color']=$params['start_color'];
        $info['end_color']=$params['end_color'];
        $info['relationId']=$params['relationId'];
        $result = $this::save($info);
        return $result;
    }

    public function get_list($params){
        $where = '';
        if($params['name'] != ''){
            if(is_numeric($params['name'])){
                $where = "id={$params['name']}";
            }else{
                $where = "name like '%{$params['name']}%'";
            }

        }
        return $this->find(array(
            $where,
            'order' => 'id desc'
        ));
    }


}
